package com.zry.study.animation

import android.content.Context
import android.support.v7.widget.*
import com.chad.library.adapter.base.BaseQuickAdapter


/**
 * Created by zry on 2017/12/16.
 */
class RecyclerViewUtilKt(private val context: Context, private val recyclerView: RecyclerView) {

    //列表
    fun initRecyclerView(boolean: Boolean) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.setHasFixedSize(true)
        //设置增加或删除条目的动画
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.isNestedScrollingEnabled = false//禁止嵌套滑动
        if (boolean) {
            recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL))
            recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        }
    }

    //方格
    fun initTableRecyclerView(tableNumber: Int, boolean: Boolean) {
        val gridLayoutManager = GridLayoutManager(context, tableNumber)
        gridLayoutManager.orientation = GridLayoutManager.VERTICAL

        recyclerView.layoutManager = gridLayoutManager
        //可选）如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true)
        //设置增加或删除条目的动画
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.isNestedScrollingEnabled = false//禁止嵌套滑动
        if (boolean) {
            recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL))
            recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        }
    }

    //瀑布流
    fun initCascadeRecyclerView(decorationNumber: Int, staggeredNumber: Int, boolean: Boolean) {
        //设置item之间的间隔
        val decoration = SpacesItemDecoration(decorationNumber)
        recyclerView.addItemDecoration(decoration)
        //解决滑动不顺畅的问题
        recyclerView.layoutManager = StaggeredGridLayoutManager(staggeredNumber, StaggeredGridLayoutManager.VERTICAL)
        recyclerView.itemAnimator = DefaultItemAnimator()
        if (boolean) {
            recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL))
            recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        }
        recyclerView.isNestedScrollingEnabled = false//禁止嵌套滑动
    }


    fun setAdapter(baseQuickAdapter: BaseQuickAdapter<*, *>) {
        recyclerView.adapter = baseQuickAdapter
    }
}